<script>
//观察者
class Observer{
    constructor(name, fn){
        this.name = name
        this.fn = fn || (() => {})
    }
}
//被观察者
class Subject{
    constructor(state){
        this.state = state
        this.observers = []
    }
    setState(val){
        this.state = val
        this.observers.forEach(item => {
            item.fn(this.state)
        })
    }
    addObserver(observer){
        if(this.observers.indexOf(observer) === -1){
            this.observers.push(observer)
        }
    }
    delObserver(observer){
        this.observers = this.observers.filter(item => item !== observer)
    }
}

let bzr = new Observer('班主任', function (){console.log(this.name,': 叫家长')})
let xz = new Observer('校长', function (){console.log(this.name,': 开除学籍')})
let gailun = new Subject('正在学习')

gailun.addObserver(bzr)
gailun.addObserver(xz)

console.log(gailun)
gailun.setState('抽烟喝酒烫头')




</script>